package com.zz.common.app;

import java.util.Collection;
import java.util.HashMap;

import android.content.Intent;
import android.content.res.Configuration;
import android.os.Bundle;
import android.view.View;
import android.widget.TabHost;
import android.widget.TabHost.OnTabChangeListener;
import android.widget.TabHost.TabContentFactory;

public class BaseFrameActivity extends BaseActivity implements TabContentFactory
			, OnTabChangeListener {
	
	private static final String INDEX_CURRENT_TAB_TAG = "current_tab_tag";
	
    protected TabHost mTabHost;
    private final HashMap<String, BaseFrame> mFrames = new HashMap<String, BaseFrame>(3);
    private BaseFrame mPreFrame;
    
	@Override
	public void onStart() {
		super.onStart();
		getCurrentFrame().onStart();
	}
	
	@Override
	public void onRestart() {
		super.onRestart();
		getCurrentFrame().onRestart();
	}
	
    @Override
	public void onResume() {
        super.onResume();
        getCurrentFrame().onResume();
    }
    
    @Override
	public void onPause() {
        super.onPause();
        getCurrentFrame().onPause();
    }
    
	@Override
	public void onStop() {
		super.onStop();
		getCurrentFrame().onStop();
	}

    @Override
	public void onDestroy() {
        super.onDestroy();
        Collection<BaseFrame> all = mFrames.values();
        for (BaseFrame frame : all) {
            frame.onDestroy();
        }
    }
    
    public void addFrame(Class<? extends BaseFrame> clz, View tab) {
        if (mTabHost == null) {
            mTabHost = (TabHost) findViewById(android.R.id.tabhost);
            mTabHost.setup();
            mTabHost.setOnTabChangedListener(this);
        }
        mTabHost.addTab(mTabHost.newTabSpec(clz.getName()).setIndicator(tab).setContent(this));
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        getCurrentFrame().onActivityResult(requestCode, resultCode, data);
    }

    @Override
    public void onConfigurationChanged(Configuration newConfig) {
        super.onConfigurationChanged(newConfig);
        Collection<BaseFrame> all = mFrames.values();
        for (BaseFrame frame : all) {
            frame.onConfigurationChanged(newConfig);
        }
    }

    @Override	//TabContentFactory
    public View createTabContent(String tag) {
        final String className = tag;
        View content = null;
        BaseFrame frame = null;
        try {
            frame = (BaseFrame) Class.forName(className).newInstance();
        } catch (Exception e) {
            return null;
        }
        frame.setActivity(this);
        content = frame.onCreateView(getLayoutInflater());
        frame.setContentView(content);
        frame.onCreate();
        mFrames.put(className, frame);
        return content;
    }

    @Override  //TabHost.OnTabChangedListener
    public void onTabChanged(String tabId) {
        if (mPreFrame != null) {
            mPreFrame.onPause();
        }
        mPreFrame = getCurrentFrame();
        mPreFrame.onResume();
    }

    protected BaseFrame getCurrentFrame() {
        return mFrames.get(mTabHost.getCurrentTabTag());
    }

    public int getCurrentTab() {
        return mTabHost.getCurrentTab();
    }
    
    @Override
    protected void onSaveInstanceState(Bundle outState) {
        super.onSaveInstanceState(outState);
        String currentTabTag = mTabHost.getCurrentTabTag();
        if (null != currentTabTag) {
            outState.putString(INDEX_CURRENT_TAB_TAG, currentTabTag);
        }
        for(BaseFrame frame: mFrames.values()) {
        	frame.onSaveInstanceState(outState);
        }
    }
    
    @Override
    protected void onRestoreInstanceState(Bundle savedInstanceState) {
        super.onRestoreInstanceState(savedInstanceState);
        String currentTabTag = savedInstanceState.getString(INDEX_CURRENT_TAB_TAG);
        if (null != currentTabTag) {
            mTabHost.setCurrentTabByTag(currentTabTag);
        }
        for(BaseFrame frame: mFrames.values()) {
        	frame.onRestoreInstanceState(savedInstanceState);
        }
    }
}